package com.thermofisher.utils;

import com.thermofisher.beans.SearchProject;
import com.thermofisher.mapper.ProjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class DataUtil
{
    @Autowired
    private ProjectMapper projectMapper;

    public long getMinData(String part)
    {
        if (StringUtils.isBlank(part))
        {
            return -1;
        }
        int strLength = part.length();
        String unit = part.substring(strLength - 1, strLength);
        if (!"M".equalsIgnoreCase(unit) && !"K".equalsIgnoreCase(unit) &&
                !"B".equalsIgnoreCase(unit))
        {
            return -1;
        }
        long data = Long.parseLong(part.substring(0, strLength - 1));
        if (StringUtils.contains(part, "M"))
        {
            data = data * 1000 * 1000;
        }
        else if (StringUtils.contains(part, "K"))
        {
            data = data * 1000 - 500;
        }
        else if (StringUtils.contains(part, "B"))
        {
            data = data * 1000 * 1000 * 1000;
        }
        return data;
    }

    public long getMaxData(String part)
    {
        if (StringUtils.isBlank(part))
        {
            return -1;
        }
        int strLength = part.length();
        String unit = part.substring(strLength - 1, strLength);
        if (!"M".equalsIgnoreCase(unit) && !"K".equalsIgnoreCase(unit) &&
                !"B".equalsIgnoreCase(unit))
        {
            return -1;
        }
        long data = Long.parseLong(part.substring(0, strLength - 1));
        if (StringUtils.contains(part, "M"))
        {
            data = data * 1000 * 1000 + (1000 * 500);
        }
        else if (StringUtils.contains(part, "K"))
        {
            data = data * 1000 + 500;
        }
        else if (StringUtils.contains(part, "B"))
        {
            data = data * 1000 * 1000 * 1000 + (1000 * 1000 * 500);
        }
        return data;
    }

    public SearchProject getSearchProject(SearchProject searchProject)
    {
        String minEstAmount = searchProject.getMinEstAmount();
        String minBooking = searchProject.getMinBooking();
        String minRevenue = searchProject.getMinRevenue();
        String maxEstAmount = searchProject.getMaxEstAmount();
        String maxBooking = searchProject.getMaxBooking();
        String maxRevenue = searchProject.getMaxRevenue();
        minEstAmount = String.valueOf(this.getMinData(minEstAmount));
        minBooking = String.valueOf(this.getMinData(minBooking));
        minRevenue = String.valueOf(this.getMinData(minRevenue));
        maxEstAmount = String.valueOf(this.getMaxData(maxEstAmount));
        maxBooking = String.valueOf(this.getMaxData(maxBooking));
        maxRevenue = String.valueOf(this.getMaxData(maxRevenue));
        searchProject.setMinEstAmount(minEstAmount);
        searchProject.setMinBooking(minBooking);
        searchProject.setMinRevenue(minRevenue);
        searchProject.setMaxEstAmount(maxEstAmount);
        searchProject.setMaxBooking(maxBooking);
        searchProject.setMaxRevenue(maxRevenue);
        return searchProject;
    }

    public String getEmail(String email)
    {
        log.info("=======email=========" + email);
        if (StringUtils.isBlank(email) || email.equalsIgnoreCase("null"))
        {
            return "";
        }
        int count = projectMapper.getEmail(email);
        if (count > 0)
        {
            return "";
        }
        return email;
    }
}
